<?php


namespace App\backend\services;

use App\backend\models\GoodsModel as Goods;
use App\common\models\Setting;
use Illuminate\Support\Facades\DB;

class SettingServices
{

    /**
     * 商城设置——查看
     */
    public static function getShop($params)
    {

        $result = Setting::getValue(0,'shop.shop');

        $result['logo'] = $result['logo']?get_host().$result['logo']:'';
        $result['poster'] = $result['poster']?get_host().$result['poster']:'';
        $result['copyrightImg'] = $result['copyrightImg']?get_host().$result['copyrightImg']:'';

        return $result?str_replace('null','',$result):'';

    }

    /**
     * 商城设置——添加/编辑
     */
    public static function setShop($params)
    {

        //站点开关(0关，1开)
        $data['switch'] = $params['switch'];
        //商城名称
        $data['name'] = $params['name'];
        //主题颜色
        $data['themeColor'] = $params['themeColor'];
        //商城LOGO
        $data['logo'] = $params['logo'];
        //商城海报
        $data['poster'] = $params['poster'];

        //版权设置
        //版权信息
        $data['copyright'] = $params['copyright'];
        //版权图片
        $data['copyrightImg'] = $params['copyrightImg'];
        //跳转链接
        $data['catAdvUrl'] = $params['catAdvUrl'];
        //小程序链接
        $data['smallCatAdvUrl'] = $params['smallCatAdvUrl'];

        //商城业绩
        //商城业绩显示开关(0关，1开)
        $data['achievementSwitch'] = $params['achievementSwitch'];
        //会员等级查看权限
        $data['memberLevel'] = $params['memberLevel'];

        //其他设置
        //客服链接
        $data['cserviceUrl'] = $params['cserviceUrl'];
        //小程序客服聊天路径
        $data['cserviceMiniUrl'] = $params['cserviceMiniUrl'];
        //百度统计
        $data['baiduStat'] = $params['baiduStat'];
        //余额字样
        $data['balance'] = $params['balance'];
        //积分字样
        $data['points'] = $params['points'];

        //微信客服设置
        //微信客服开关(0关，1开)
        $data['wxCustomerServiceSwitch'] = $params['wxCustomerServiceSwitch'];
        //企业ID(corpId)
        $data['corpId'] = $params['corpId'];

        $result = Setting::setValue(0,'shop.shop',$data);

        return $result?$result:'';

    }

    /**
     * 订单设置——查看
     */
    public static function getOrder($params)
    {

        $result = Setting::getValue(0,'shop.order');

        if (!empty($result['goods'])) {
            $goods = DB::table('goods')->select('id', 'title', 'thumb')
                ->where('status', 1)
                ->whereIn('id', $result['goods'])
                ->where('plugin_id', 0)
                ->get()->toArray();
            if (!$goods) {
                $goods->map(function ($query) {
                    return $query->thumb_url = get_host().$query->thumb;
                });
            }
        } else {
            $goods = [];
        }

        $result['goodsData'] = array_values($goods);
        return $result?str_replace('null','',$result):'';

    }

    /**
     * 订单设置——添加/编辑
     */
    public static function setOrder($params)
    {

        //按商品拆单开关(0关，1开)
        $data['orderApartSwitch'] = $params['orderApartSwitch'];
        //首单商品
        $data['goods'] = $params['goods'];
        //催发货开关(0关，1开)
        $data['eDeliverySwitch'] = $params['eDeliverySwitch'];
        //调用微信地址(0关，1开)
        $data['wxAddressSwitch'] = $params['wxAddressSwitch'];
        //自定义文案
        $data['customCopy'] = $params['customCopy'];
        //运费说明开关(0关，1开)
        $data['freightDescSwitch'] = $params['freightDescSwitch'];
        //运费说明
        $data['freightDesc'] = $params['freightDesc'];
        //多包裹前端显示开关(0关，1开)
        $data['packageShowSwitch'] = $params['packageShowSwitch'];
        //下单跳转表单填写开关(0关，1开)
        $data['formJumpSwitch'] = $params['formJumpSwitch'];

        $result = Setting::setValue(0,'shop.order',$data);

        return $result?$result:'';

    }

    /**
     * 物流设置——查看
     */
    public static function getExpress($params)
    {

        $result = Setting::getValue(0,'shop.express');
        return $result?$result:'';

    }

    /**
     * 物流设置——添加/编辑
     */
    public static function setExpress($params)
    {

        //AppId
        $data['appId'] = $params['appId'];
        //AppSecret
        $data['appSecret'] = $params['appSecret'];

        $result = Setting::setValue(0,'shop.express',$data);

        return $result?$result:'';

    }

    /**
     * 短信设置——查看
     */
    public static function getSms($params)
    {

        $result = Setting::getValue(0,'shop.sms');
        return $result?$result:'';

    }

    /**
     * 短信设置——添加/编辑
     */
    public static function setSms($params)
    {

        $data['type'] = $params['type'];
        //type 1:阿里云，2:腾讯云
        //AccessKeyId
        $data['appkey'] = $params['appkey'];
        //AccessKeySecret
        $data['secret'] = $params['secret'];
        //短信签名
        $data['signname'] = $params['signname'];
        //注册模板编号
        $data['tempCode'] = $params['tempCode'];
        //找回密码模板编号
        $data['tempCodeForget'] = $params['tempCodeForget'];
        //登录模板编号
        $data['tempCodeLogin'] = $params['tempCodeLogin'];
        //余额定时提醒
        $data['tempBalanceCode'] = $params['tempBalanceCode'];
        //商品发货提醒
        $data['tempSendMessageCode'] = $params['tempSendMessageCode'];
        //会员充值提醒
        $data['tempChargeCode'] = $params['tempChargeCode'];

        $result = Setting::setValue(0,'shop.sms',$data);

        return $result?$result:'';

    }

    /**
     * 联系方式——查看
     */
    public static function getContact($params)
    {

        $result = Setting::getValue(0,'shop.contact');
        return $result?$result:'';

    }

    /**
     * 联系方式——添加/编辑
     */
    public static function setContact($params)
    {

        $data = [
            'phone' => $params['phone'], //客服电话
            'address' => $params['address'], //所在地址
            'description' => $params['description'], //商城简介
            'storeName' => $params['storeName'], //店铺名称
            'storeAddress' => $params['storeAddress'], //店铺地址
            'provinceId' => $params['provinceId'], //店铺位置——省
            'cityId' => $params['cityId'], //店铺位置——市
            'districtId' => $params['districtId'], //店铺位置——区
            'streetId' => $params['streetId'], //店铺位置——街道
            'storeLongitude' => $params['storeLongitude'], //店铺定位——经度
            'storeLatitude' => $params['storeLatitude'] //店铺定位——纬度
        ];

        $result = Setting::setValue(0,'shop.contact',$data);

        return $result?$result:'';

    }

    /**
     * 优惠券——查看
     */
    public static function getCoupon($params)
    {

        $result = Setting::getValue(0,'shop.coupon');
        return $result?$result:'';

    }

    /**
     * 优惠券——添加/编辑
     */
    public static function setCoupon($params)
    {

        $data = [
            'delayed' => $params['delayed'], //优惠券到期消息通知
            'everyDay' => $params['everyDay'], //提醒时间
            'expireSwitch' => $params['expireSwitch'], //优惠券过期提醒开关(0关，1开)
            'showCouponSwitch' => $params['showCouponSwitch'], //券后价和商品优惠券显示开关(0关，1开)
            'couponRemindSwitch' => $params['couponRemindSwitch'], //下单提示使用优惠券开关(0关，1开)
        ];

        $result = Setting::setValue(0,'shop.coupon',$data);

        return $result?$result:'';

    }

    /**
     * 分享——查看
     */
    public static function getShare($params)
    {

        $result = Setting::getValue(0,'shop.share');

        $result['followImg'] = $result['followImg']?get_host().$result['followImg']:'';
        $result['shareIcon'] = $result['shareIcon']?get_host().$result['shareIcon']:'';

        return $result?str_replace('null','',$result):'';

    }

    /**
     * 分享——添加/编辑
     */
    public static function setShare($params)
    {

        $data = [
            'followSwitch' => $params['followSwitch'], //启用会员关注开关(0关，1开)
            'type' => $params['type'], //关注引导页1：链接，2：图片
            'followUrl' => $params['followUrl'], //关注引导页—链接
            'followImg' => $params['followImg'], //关注引导页—图片
            'shareTitle' => $params['shareTitle'], //分享标题
            'shareIcon' => $params['shareIcon'], //分享图标
            'shareDesc' => $params['shareDesc'], //分享描述
        ];

        $result = Setting::setValue(0,'shop.share',$data);

        return $result?$result:'';

    }

    /**
     * 地图设置——查看
     */
    public static function getMap($params)
    {

        $result = Setting::getValue(0,'shop.map');
        return $result?$result:'';

    }

    /**
     * 地图设置——添加/编辑
     */
    public static function setMap($params)
    {

        $data = [
            'webKey' => $params['webKey'], //web服务
            'webApi' => $params['webApi'], //web端(JS API)
            'webSecret' => $params['webSecret'], //web端(JS API) 安全密钥
        ];

        $result = Setting::setValue(0,'shop.map',$data);

        return $result?$result:'';

    }

    /**
     * 邮箱配置——查看
     */
    public static function getEmail($params)
    {

        $result = Setting::getValue(0,'shop.email');
        return $result?$result:'';

    }

    /**
     * 邮箱配置——添加/编辑
     */
    public static function setEmail($params)
    {

        $data = [
            'emailType' => $params['emailType'], //邮箱类型1:QQ邮箱，2：163邮箱
            'senderEmail' => $params['senderEmail'], //发件人邮箱
            'emailAuthCode' => $params['emailAuthCode'], //邮箱授权码
        ];

        $result = Setting::setValue(0,'shop.email',$data);

        return $result?$result:'';

    }

    /**
     * 支付方式——查看
     */
    public static function getPay($params)
    {

        $result = Setting::getValue(0,'shop.pay');
        return $result?str_replace('null','',$result):'';

    }

    /**
     * 支付方式——添加/编辑
     */
    public static function setPay($params)
    {
        //微信支付是否开启开关(0关，1开)
        $data['weixinSwitch'] = $params['weixinSwitch'];
        //微信付款码支付开关(0关，1开)
        $data['wechatMicroSwitch'] = $params['wechatMicroSwitch'];
        //微信扫码支付开关(0关，1开)
        $data['wechatNativeSwitch'] = $params['wechatNativeSwitch'];
        //身份标识(appId)
        $data['weixinAppid'] = $params['weixinAppid'];
        //身份密钥(appSecret)
        $data['weixinSecret'] = $params['weixinSecret'];
        //微信支付商户号(mchId)
        $data['weixinMchid'] = $params['weixinMchid'];
        //微信支付密钥(apiSecret)
        $data['weixinApisecret'] = $params['weixinApisecret'];
        //微信支付证书-CERT证书文件
        $data['weixinCert'] = $params['weixinCert'];
        //微信支付证书-KEY密钥文件
        $data['weixinKey'] = $params['weixinKey'];
        //余额支付-是否开启开关(0关，1开)
        $data['balancePaySwitch'] = $params['balancePaySwitch'];
        //货到付款-是否开启开关(0关，1开)
        $data['codSwitch'] = $params['codSwitch'];
        //微信支付-V3新版-是否开启开关(0关，1开)
        $data['weixinApiv3Switch'] = $params['weixinApiv3Switch'];
        //微信支付-V3新版-微信apiV3密钥
        $data['weixinApiv3Secret'] = $params['weixinApiv3Secret'];

        $result = Setting::setValue(0,'shop.pay',$data);

        return $result?$result:'';

    }

}
